/**
* @Title: caseAuditList_Ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('ProblemRegisterCtrl', ['$scope','$state','$http','$modal','CaseAuditListService','CaseCommonService',
	                                           'BaseURL','Modal','FwaTabsetService','AuditDictServices','InsuranceService','CaseAuditAddService',
	                                function($scope,$state,$http,$modal,caseAuditListService,CaseCommonService,
	                                		baseUrl,Modal,fwaTabsetService,AuditDictServices,InsuranceService,CaseAuditAddService){
		$scope.addOnClick = function(){
			fwaTabsetService.selectTab('problemRegister.addProblemInfo',null,false);
		}
		
		$scope.auditDictServices = AuditDictServices;
		AuditDictServices.initCodeListDatas();

		InsuranceService.initMedicalOrgInfoList().success(function(response){
            $scope.medicalOrgInfoList = response.medicalOrgList;
		});
		
		
        var cellTemplate = '<a ng-show="data.problemStatus ==0" ng-click="operation(data);">提交</a>' +
            '<a ng-show="data.problemStatus == 2" ng-click="operation(data);">初审</a>'+
            '<a ng-show="data.problemStatus == 3" ng-click="operation(data);">撤回</a>';
		$scope.columns = [
  		{
			label : '事件号',
			map : 'problemCode',
			width : '10%',
			 cellTemplate:'<a  ng:bind="data.problemCode" '+
			 	'ng-click="queryByProblemCode(data)"></a>'}, 
		{
			label : '事件时间段',
			map : 'periodDate',
			width : '15%'
		}, {
			label : '事件要点',
			map : 'keyPoint',
			width : '20%'
		}, {
			label : '来源方式',
			map : 'sourceType',
			width : '10%'
		}, {
			label : '提交日期',
			map : 'submitDate',
			width : '15%'
		}, {
			label : '关联案件号',
			map : 'otherCaseCode',
			width : '10%'
		}, {
			label : '状态',
			map : 'statusName',
			width : '10%'
		}, {
			label : '操作',
			map : 'operation',
			width : '10%',
			cellTemplate:cellTemplate}];
		
		$scope.operation = function(data){
			/*if(data.problemStatus =='3' ){
				fwaTabsetService.selectTab('problemRegister.problemInfoView',{problemCode:data.problemCode},false);
			}else */
            if(data.problemStatus == '0' ){//事件暂存
				$state.go('problemRegister.addProblemInfo',{problemCode:data.problemCode});
			}else if(data.problemStatus == '2' ){//事件初审
				$state.go('problemRegister.problemFirstCheck',{problemCode:data.problemCode});
			}else if(data.problemStatus == '3' ){//撤回事件初审
				$state.go('problemRegister.problemFirstCheck',{problemCode:data.problemCode});
			}
		}
		
		$scope.queryByProblemCode = function(data){
            $state.go('problemRegister.problemInfoView',{problemCode:data.problemCode,problemStatus:data.problemStatus});
		}
		

		$scope.onReset = function(){
				$scope.params.problemCode=null;
	        	$scope.params.orgName=null;
                $scope.params.sourceType=null;
                $scope.params.hearDate={start:null,end:null};
	        	$scope.params.doctorName=null;
	        	$scope.params.jobLicense=null;
	        	$scope.params.sbmitDate={start:null,end:null};
	        };

        $scope.currentPage = 1;
        var start = 0;
        $scope.limit = 20;
        $scope.params = {
            page:0,
            pageSize:20
        };

        //分页监听
        $scope.$on('pageTool.skipPage',function(e,v){
            $scope.currentPage = v;
            start = $scope.limit*(v-1);
            $scope.onSubmit(true);
        });
        /**
	     * 
	     **/
	    $scope.onSubmit = function(resetPage){
	    	if(resetPage == null || !resetPage){
	    		$scope.currentPage= 1;
	    		$scope.params.page = 0; 
	    	}else{
    			$scope.params.page = ($scope.currentPage - 1) * 20;
	    	}
            if($scope.params.problemCode == ""){
            	$scope.params.problemCode = null;
            }
            if($scope.params.jobLicense == ""){
            	$scope.params.jobLicense = null;
            }
            if($scope.params.doctorName == ""){
            	$scope.params.doctorName = null;
            }
            
	    	var fparam = $scope.params;
	    	if($scope.params.sbmitDate){
		    	var start = $scope.params.sbmitDate.start;
		    	var end = $scope.params.sbmitDate.end;
		    	fparam.start = start;
		    	fparam.end = end;
	    	}
	    	
            if($scope.params.hearDate){
                var hearDateStart = $scope.params.hearDate.start;
                var hearDateEnd = $scope.params.hearDate.end;
                fparam.hearDateStart = hearDateStart;
                fparam.hearDateEnd = hearDateEnd;
            }
	    	fparam.problemStatus ="\'0\',\'2\',\'3\'";
	    	CaseCommonService.loadProblemList4Reg(fparam).success(function(response){
	    		if(response){
            		$scope.tableData = response.root;
        			$scope.totalCount = response.total;
	    		}
	    	});
	    
	    }
	    
	    var checkRows = [];
        function findCheck(d) {
            for(var i=0;i< checkRows.length;i++) {
                var checkedData =  checkRows [i];
                if(checkedData.problemCode == d.problemCode) {
                    return i;
                }
            }
            return -1;
        }

       $scope.$on('fwatable.checked', function(e,d){
            if(findCheck(d) == -1) {
                checkRows.push(d);
            }
        });

        $scope.$on('fwatable.unchecked', function(e,d){
            var index = findCheck(d);
            if(index != -1) {
               checkRows.splice(index,1);
            }
        });

        //---------------开始-----------------
        //批量操作（调查）
        $scope.loading = false;
        /**
	     * 批量提交
	     */
        $scope.onBatchOperate = function(){
        	var problemCodes= [];
        	var alertMsg ="";
        	if(checkRows.length <= 0){
                Modal.alert('请选择需要操作的选项！');
                return;
	        }else{
	        	//验证勾选项是否全为调查
	        	for ( var i = 0; i < checkRows.length; i++) {
	        		if(checkRows[i].problemStatus== "3"){
	        			alertMsg+=checkRows[i].problemCode +" 当前状态："+checkRows[i].statusName+"；\n";
	        			continue;
	        		}
	        		problemCodes.push(checkRows[i].problemCode);
	        	}
//	        	$scope.loading = true;
	        	if(problemCodes.length == 0 ){
	        		Modal.alert("当前所选择数据状态不符合批量提交或刷新页面再操作！");
	        		return;
	        	}
	        	if(problemCodes.length !=checkRows.length ){
	        		"所选择数据"
	        	}
	        	var parameter={problemCodes:problemCodes};
	        	Modal.confirm('确认批量操作吗？',function(){ 
	        		CaseAuditAddService.batchEventsInitWf(parameter).success(function(r){
	        			Modal.alert(r.msg);
	        			$scope.onSubmit();
	        			$modalInstance.dismiss();
	        		});
			  });
	        }
        } 
	    
	    
	    
	    $scope.onSubmit();
	// end line	
	}]);
})();
